How to use LIKE in a CASE statement in Transact SQL query
I have a text field that I want to search for the abbreviation DNF (Do not Fill in medical jargon) which may be anywhere in the note; I can't come up with the right syntax for a query in MS SQL server 2008.
I put in
CASE dbo.PTC_MEDICATION.NOTES WHEN LIKE '%DNF%' THEN 'DNF' ELSE ' ' END
and the server changes it to
'CASE dbo.PTC_MEDICATION.NOTES WHEN LIKE %DNF% DNF ELSE END'
which does not work
nor does any of several variations; I've tried with and without the % adding parentheses
It seems there has to be a LIKE clause as if the statement is just:
CASE dbo.PTC_MEDICATION.NOTES WHEN '%DNF%' THEN 'DNF' ELSE '' END
the query does nnot pick up the value if anything else is in the string
Any suggestions?
Thanks,
Chris
June 29th, 2012 11:59am
Hello Chris,
Probably you can seek help in concerning SQL forums:http://social.msdn.microsoft.com/Forums/en/transactsql/threadsRegards, Ravikumar P
Free Windows Admin Tool Kit Click here and download it now
June 29th, 2012 12:58pm
Hello Chris,
Probably you can seek help in concerning SQL forums:http://social.msdn.microsoft.com/Forums/en/transactsql/threadsRegards, Ravikumar P
June 29th, 2012 12:58pm
Chris,
can you try the query with the case using the following syntax:
CASE WHEN dbo.PTC_MEDICATION.NOTES LIKE '%DNF%' THEN 'DNF' ELSE ' ' END
^--Moved WHEN to the left of the column name
Thanks,
DougDoug Kentner
Free Windows Admin Tool Kit Click here and download it now
June 29th, 2012 1:49pm